﻿using System;
using System.Collections.Generic;
using System.Text;

using LdtDataEngin.ModuleDetail.BaseModule;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.DataModel;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using LdtJudge.DataEngine.Model.LogicblockServers;
using Newtonsoft.Json;

namespace LdtDataEngin.ModuleDetail.ParagraphAnalysis
{
    public class ParagraphAI
    {
        // 上下文
        private DataContext _dataContext;

        private string _modelName = "paragraph";

        public ParagraphAI(DataContext dataContext)
        {
            _dataContext = dataContext;
        }
        /// <summary>
        /// 机器学习提取段落内容
        /// </summary>
        /// <param name="configStateModel"></param>
        /// <param name="modelName"></param>
        /// <returns></returns>
        public DyData_Analysis AIExtract()
        {
            // ML返回结果
            PiecewiseResults results = new PiecewiseResults();

            // ML执行后得到的段落结果
            List<ParsingCount> resultsList = new List<ParsingCount>();

            // 机器学习对象
            MQDataSource mQDataSource = new MQDataSource(ConfigRetrieval.GetConfigRetrieval.GetConnectionString("IntelligenceParagraphAnalysisMQ"));

            // 调用机器学习方法
            string MLmessage = mQDataSource.AIExtract(_dataContext.DataLayerAns.FengXiCanShu.Juanzongneirong, _dataContext.DataLayerAns.SourceCode.ToLower());
            results = JsonConvert.DeserializeObject<PiecewiseResults>(MLmessage);
            if (results.Result.Code == "1")
            {
                resultsList = JsonConvert.DeserializeObject<List<ParsingCount>>(results.Body.ToString());
            }

            // 将结果转化成到动态数据中的段落列表中
            List<An_duanluo> convertResult = new List<An_duanluo>();
            foreach (ParsingCount item in resultsList)
            {
                An_duanluo duanluo = new An_duanluo();
                duanluo.Duanluoid = Snowflake.Instance().GetId().ToString();
                duanluo.Duanluobianhao = item.ParagraphCode.ToLower();
                foreach (Contents content in item.Body)
                {
                    duanluo.Duanluoneirong += content.Conunt;
                }
                convertResult.Add(duanluo);
            }
            _dataContext.DataLayerAns.Duanluos = convertResult;
            return _dataContext.DataLayerAns;
        }
    }
}
